package com.dapeis.web.admin.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.ag.core.base.Pager;
import com.ag.dao.PagerHelper;
import com.dapeis.core.convert.pager.OrderPagerSearchConvertCriteria;
import com.dapeis.core.utils.RequirementConfig;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Order;
import com.dapeis.domain.ext.OrderExt;
import com.dapeis.domain.ext.TaskOrderExt;
import com.dapeis.service.CollocationPlanService;
import com.dapeis.service.OrderService;
import com.dapeis.service.TaskService;

@Controller
@RequestMapping("/admin/order")
public class AdminOrderController extends AdminController {

    @Resource
    OrderService orderService;
    @Resource
    TaskService taskService;
    @Resource
    CollocationPlanService planService;

    @RequestMapping("/awardList")
    public String awardList(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	String pagerUrl = "order/awardList";
	return doList(StylistConstants.Order.Type.AWARD_PROGRAM, pager, model,
		attr, pagerUrl);
    }

    @RequestMapping("/requirment")
    public String requirementList(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	String pagerUrl = "order/requirementList";
	return doList(StylistConstants.Order.Type.REQUIREMENT, pager, model,
		attr, pagerUrl);
    }

    @RequestMapping("/refund")
    public String refundList(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	if (null == pager) {
	    pager = new Pager();
	}
	pager.init(OrderExt.class, new OrderPagerSearchConvertCriteria());
	Example example = new Example(TaskOrderExt.class);
	Criteria criteria = example.createCriteria();
	criteria.andEqualTo(Order.PROPERTY_SOURCE,
		StylistConstants.OrderSource.WEIXIN_USER);
	criteria.andEqualTo(Order.PROPERTY_STATE,
		StylistConstants.Order.Status.REFUND);
	example.orderBy(Order.PROPERTY_RECORDCREATETIME).desc();
	pager.setExample(example);
	taskService.pager(pager);
	orderService.doQueryOrderRefundResult(pager);
	// PagerHelper.pager(pager, orderService.getOrderExtMapper());
	model.put("pager", pager);
	return doPackageURI("order/refund");
    }

    public String doList(String type, Pager pager, Map<Object, Object> model,
	    RedirectAttributes attr, String pagerUrl) {

	if (null == pager) {
	    pager = new Pager();
	}
	pager.init(OrderExt.class, new OrderPagerSearchConvertCriteria());
	Example example = new Example(TaskOrderExt.class);
	Criteria criteria = example.createCriteria();
	criteria.andEqualTo(Order.PROPERTY_TYPE, type.toString());
	criteria.andEqualTo(Order.PROPERTY_SOURCE,
		StylistConstants.OrderSource.WEIXIN_USER);
	example.orderBy(Order.PROPERTY_RECORDCREATETIME).desc();
	pager.setExample(example);
	taskService.pager(pager);
	if (type.equals(StylistConstants.Order.Type.AWARD_PROGRAM)) {
	    // 此处设计有问题,后续重构
	    doInitPlanData(pager);
	}
	// PagerHelper.pager(pager, orderService.getOrderExtMapper());
	model.put("pager", pager);
	return doPackageURI(pagerUrl);
    }

    
    void doInitPlanData(Pager pager) {
	if (!(pager.getItemList().isEmpty())) {
	    for (Iterator iter = pager.getItemList().iterator(); iter.hasNext();) {
		TaskOrderExt taskOrderExt = (TaskOrderExt) iter.next();
		if (null != taskOrderExt
			&& taskOrderExt.getType().equals(
				StylistConstants.Order.Type.AWARD_PROGRAM)) {
		    taskOrderExt.setPlan(planService
			    .selectByPrimaryKey(taskOrderExt.getLinkId()));
		    taskOrderExt.setRequirement(null);
		}
	    }
	}
    }
}
